package com.example.bbs.controller;


import com.example.bbs.domain.model.Page;
import com.example.bbs.domain.model.PostVO;
import com.example.bbs.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * 社区首页接口
 */
@Controller
public class IndexController {

    @Autowired
    private PostService postService;

    @GetMapping("/")
    public String theLatestPosts(Model model, Page page) {
        processingResponse(model, page,
                postService.getTheLatestPostInfoList(page.getPage(), page.getLimit()),
                0, "/index/theLatestPosts");
        return "index";
    }

    @GetMapping("/index/theHottestPosts")
    public String theHottestPosts(Model model, Page page) {
        processingResponse(model, page,
                postService.getTheHottestPostInfoList(page.getPage(), page.getLimit()),
                1, "/index/theHottestPosts");
        return "index";
    }

    @GetMapping("/index/dayHotPostRank")
    public String toDayHotPosts(Model model, Page page) {
        processingResponse(model, page, postService.getDayHotPostRank(),
                2, "/index/dayHotPostRank");
        return "index";
    }

    @GetMapping("/index/weekHotPostRank")
    public String lastWeekHotPosts(Model model, Page page) {
        processingResponse(model, page, postService.getWeekHotPostRank(),
                3, "/index/weekHotPostRank");
        return "index";
    }

    /**
     * 封装返回信息
     */
    private void processingResponse(Model model, Page page, PostVO postVO, int flag, String uri) {
        page.setPath(uri);
        page.setTotalSize(postVO.getTotalSize());

        model.addAttribute("posts", postVO);
        model.addAttribute("flag", flag);

    }
}
